
#ifndef __DATAAPI_H__
#define __DATAAPI_H__

#include "utils.h"
#include "tproto.h"
#include "strlist.h"

#define ERROR_OK					0	

//本系统（http_dataapi.h专用）
#define ERROR_AUTH					-1
#define ERROR_REALM					-2
#define ERROR_URL					-3
#define ERROR_SOCKET				-4
#define ERROR_QUERY_EMPTY			-5
#define ERROR_HANDLE				-6
#define ERROR_TIMEOUT_D				-7
#define ERROR_SOCKET_SEND			-8
#define ERROR_SOCKET_RECV			-9

//固件系统（dataapi.h专用）

#define ERROR_PARAM					-100 
#define ERROR_QUERY					-101 
#define ERROR_TABLE					-102 
#define ERROR_FIELDS				-103 
#define ERROR_OPTIONS				-104 
#define ERROR_IO					-105 
#define ERROR_NO_MEM				-106 
#define ERROR_UPDATE_DATA			-107 
#define ERROR_UPDATE_PK				-108 
#define ERROR_FILTER_FIELD			-109 
#define ERROR_FILTER_OP				-110 
#define ERROR_FILTER_VAL_TIME		-111 
#define ERROR_UPDATE_OLDDATA		-112 
#define ERROR_UPDATE_SIZE			-113 
#define ERROR_UPDATE_ERROR			-114 
#define ERROR_UPDATE_NO_PK			-115
#define ERROR_UPDATE_NOUSER			-116
#define ERROR_UPDATE_FIELD			-117
#define ERROR_NO_SPACE				-118
#define ERROE_DELETE_SAVE			-119



//Device Data Access Uni-Interface
int dataURI(const char *query, TBuffer *buffer);

#define FREE_SPCACE		(100*1024)



///--------------------------------------------------------------------




///////////////////运算符串描述////////////////////////////
#define STR_OP_NULL					("")
#define STR_OP_EQU					("=")
#define STR_OP_IN					("IN")	
#define STR_OP_BETWEN				("BETWEN")
#define STR_OP_LESS_THAN			("<")      
#define STR_OP_MORE_THAN			(">")	   
#define STR_OP_LESS_THAN_EQU		("<=") 
#define STR_OP_MORE_THAN_EQU		(">=") 
#define STR_OP_LIKE					("LIKE") 
#define STR_OP_HAS					("HAS") 

///////////////////运算符取值////////////////////////////
#define BIT_OP_NULL					(0)
#define BIT_OP_EQU					(1)   
#define BIT_OP_IN					(2)   
#define BIT_OP_BETWEN				(3)   
#define BIT_OP_LESS_THAN			(4)   
#define BIT_OP_MORE_THAN			(5)   
#define BIT_OP_LESS_THAN_EQU		(6)   
#define BIT_OP_MORE_THAN_EQU		(7)   
#define BIT_OP_LIKE					(8)   
#define BIT_OP_HAS					(9) 

//////////////////运算符串 <----> 值 对应关系/////////////////
typedef struct  
{
	int			nOP;
	char		strOP[50];
}MAP_BIT_OP;




//各种传入的过滤选项集合
typedef struct
{
	int		nMaxReturn;
	bool	bIgnoreCase;
	bool	bIgnoreNewField;
	bool	bUpdateOldData;
	bool	bErrorBreak;
	bool	bIgnoreSizeError;
}OPTION_SET;

//
// 以下为字段名回调函数
//
//	get 类型
typedef int (*FldValueFunT)(void *data, char *valueBuffer, int bufferSize);
//	set 类型
typedef int (*FldSetFunT)(void *data, char *valueBuffer, int bufferSize);
//	check 类型
typedef int (*FldCheckFunT)(void *data);



//
// 以下为表使用回调函数
//
//the callback function for test a rowData is taged deleted(return 1) or not(return 0)
typedef int (*TblIsDeletedFunT)(void *rowData);
//the callback function for doing delete operation for a rowData in a table.
//return 1 for overwrite old record, return 0 for not overwrite
typedef int (*TblDelRowFunT)(void *rowData);
typedef int (*TblRepRecord)(void *data, int flag, int primaryKey);
typedef int (*TblGetAllPK)(void *dataIn, void *dataOut, int *size);
typedef int (*TblSetInit)(void *data);


//
//	使用数据类型运算符类型
//
typedef enum {CHAR_T, STRING_T, INT_T, UINT_T, TIME_T} DATA_TYPE;
typedef enum {NO_OP, NOT, AND, OR}OPT;

//
//	过滤条件的每一个组描述，例如对于 "pin=98"
//
typedef struct 
{
	char		*fieldName;			//过滤条件中的字段名，"pin=98"中的"pin"
	int			nOP;				//过滤条件中的运算符，"pin=98"中的"="
	char		strVal[128];		//过滤条件中的第一个值，"pin=98"中的"98"
	char		strVal2[128];		//过滤条件中的第二个值，"pin in (34,89)"中的"89"
	int			nValCount;			//过滤条件中值的个数
	void		*fieldDesc;			//保存指向字段描述结构的指针
}FILTER_ITEM;

//
//	过滤条件描述，例如对于 "pin=98 AND name like  Wang*"
//
typedef struct  
{
	FILTER_ITEM		flter_item[2];	//假设不超过2个子项，"pin=98 AND name in(hello, world)"是2项
	OPT				op;				//连接运算符, 分别是“无运算符(NO_OP)，取反(NOT)，同时(AND)，或者(OR)”
	int				itemNum;		//项的个数	
}FILTER_T;



/////////////////////////////////////////////////////////////////////////////////
//分析Options
int GetOptions(OPTION_SET *pOptionSet, const char *str);
void setDefaultOptions(OPTION_SET *pOptionSet);



//一般字符串操作函数
void SkipSpace(char **pptr);		//略去前面空格
void DelLastSpace(char *pptr);		//略去最后空格
char *trimSpace(char **pptr);
char *StrRev(char *str);			//反转串
bool IsWordTrail(char ch);
bool IsOpTrail(char ch);
void SkipChar(char **pptr, char ch);
void DelLastChar(char *pptr, char ch);
char *trimChar(char **pptr, char ch);
//bool checkTimeStr1(const char *timeStr);

int GetCardFromStr(char *str, unsigned char *Card);			//从"HEX:31323334"中取出 1234

int formatTime(int stamp, char *buffer);

bool ProcessCompareInt(int *fieldVal, int op, int *val, int count);
bool ProcessCompareTime(int *fieldVal, int op, int *val, int count);
bool ProcessCompareChar(char *fieldVal, int op, char *val, char *val2);
bool ProcessCompareCaseChar(char *fieldVal, int op, char *val, char *val2);


char *get_var(const char *name, const char *buf, size_t buf_len);


#endif


